package com.fwj.portal.service.notice;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.common.CommonUtils;
import com.fwj.core.common.constant.DataConstant;
import com.fwj.core.common.execption.CommonException;
import com.fwj.core.common.model.PageRequest;
import com.fwj.core.common.model.PageResponse;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.core.common.model.ResponseEnum;
import com.fwj.portal.api.notice.NoticeService;
import com.fwj.portal.dao.notice.NoticeMapper;
import com.fwj.portal.entity.notice.Notice;
import com.fwj.portal.model.notice.NoticeVo;
import com.fwj.user.entity.user.UserBaseInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.List;

/**
 * @Author: fwj
 * @Data: 2022/4/24 0:02
 * @Version 1.0
 * @Description:
 */

@Service
public class NoticeServiceImp implements NoticeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeServiceImp.class);

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public ResponseDTO pushNotice(Notice notice, UserBaseInfo userBaseInfo) {
        try {
            Notice execNotice = buildNotice(notice, userBaseInfo);
            noticeMapper.insertSelective(execNotice);
            return new ResponseDTO().success();
        } catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.SERVER_ERROR);
        }
    }

    private Notice buildNotice(Notice notice, UserBaseInfo userBaseInfo) {
        Notice ret = new Notice();
        ret.setNoticeNumber(CommonUtils.generateUniqueNumber());
        ret.setTitle(notice.getTitle());
        ret.setContent(notice.getContent());
        ret.setLevel(notice.getLevel());
        ret.setReleaseDate(notice.getReleaseDate());
        ret.setCreator(userBaseInfo.getNumber());
        ret.setCreateTime(new Timestamp(System.currentTimeMillis()));
        ret.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        ret.setStatus(DataConstant.NORMAL);
        return ret;
    }

    @Override
    public ResponseDTO editNotice(Notice notice) {
        try {
            Example example = new Example(Notice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("noticeNumber",notice.getNoticeNumber());
            noticeMapper.updateByExampleSelective(notice,example);
            return new ResponseDTO().success();
        } catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseDTO deleteNotice(Long noticeNumber) {
        try {
            Example example = new Example(Notice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("noticeNumber",noticeNumber);
            noticeMapper.deleteByExample(example);
            return new ResponseDTO().success();
        } catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.SERVER_ERROR);
        }
    }

    @Override
    public JSONObject getNoticeDetails(Long noticeNumber) {
        try {
            Example example = new Example(Notice.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("noticeNumber",noticeNumber);
            Notice notice = noticeMapper.selectOneByExample(example);
            NoticeVo noticeVo = JSONObject.parseObject(JSONObject.toJSONString(notice), NoticeVo.class);
            return new ResponseDTO().success().setData(noticeVo);
        } catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.SERVER_ERROR);
        }
    }

    @Override
    public JSONObject queryNoticeList(PageRequest pageRequest) {
        try {

            Example example = buildNoticeExample();
            PageHelper.startPage(pageRequest.getPage(),pageRequest.getPageSize());
            PageInfo<Notice> notices = new PageInfo<>(noticeMapper.selectByExample(example));
            List<NoticeVo> noticeVo = JSONArray.parseArray(JSONObject.toJSONString(notices.getList()),NoticeVo.class);
            PageResponse pageResponse = new PageResponse();
            pageResponse.setResponseBody(noticeVo);
            pageResponse.setPageTotal(notices.getTotal());
            return new ResponseDTO().success().setData(pageResponse);
        } catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.SERVER_ERROR);
        }
    }

    private Example buildNoticeExample() {
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        criteria.andLessThanOrEqualTo("releaseDate",timestamp);
        criteria.andEqualTo("status",DataConstant.NORMAL);
        return example;
    }
}
